package com.hnx.admin.aurora.aspects;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hnx.admin.aurora.annotation.Pager;
import com.hnx.admin.aurora.web.json.PageEntity;
import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.List;

/*
创建人： 倔强的头发
创建时间：2025/6/19
功能描述:

*/
@Aspect
@Component
@RequiredArgsConstructor
public class PageAspect {
    @Before(value = "@annotation(pager)&&args(page)")
    public void toBefore(JoinPoint jp, Pager pager, PageEntity page){
        PageHelper.startPage(page.getPageNum(),page.getPageSize(),pager.orderBy());
    }
    @AfterReturning(value = "@annotation(pager)",returning = "json")
    public ResultJson toAfter(JoinPoint jp, Pager pager, ResultJson json){
        if("200".equals(json.get("code").toString())){
            List<?> list = (List<?>) json.get("rows");
            PageInfo<?> info = new PageInfo<>(list);
            json.add("total",info.getTotal());
        }
        return json;
    }
}
